﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml;

namespace DemoGame1
{
    public class StartMenu : Menu
    {
        public static string BACKGROUND = "BACKGROUND";
        public static string STARTMENUITEM = "STARTMENUITEM";

        KeyboardState oldState;

        MyButton background;
        MyButton pointer;
        int _menuItems = 0;
        Color Normal = Color.Brown;
        Color Hilite = Color.Red;

        private int selectedIndex = 0;
        myGame game;

        public StartMenu(myGame _g, String strXML)
        {
            game = _g;
            myButtons = new List<MyButton>();
            XmlDocument doc = new XmlDocument();
            doc.Load(strXML);
            XmlElement backGround = (XmlElement)doc.SelectSingleNode("//BackGround");
            //MyButton backGroundButton;
            background = new MyButton(game.Content, backGround);
            background.ButtonName = StartMenu.BACKGROUND;
            //add menu
            XmlElement buttons = (XmlElement)doc.SelectSingleNode("//Buttons");
            foreach (XmlNode button in buttons.ChildNodes)
            {
                MyButton bt = new MyButton(game.Content, (XmlElement)button);
                bt.ButtonName = StartMenu.STARTMENUITEM;
                myButtons.Add(bt);
            }
            int t = buttons.ChildNodes.Count;
            pointer = myButtons[myButtons.Count - 1];
            myButtons.RemoveAt(myButtons.Count - 1);
            _menuItems = myButtons.Count;
        }

        public override void HandleKeyInput()
        {
            KeyboardState newState = Keyboard.GetState();
            if (newState.IsKeyDown(Keys.Down))
            {
                if (!oldState.IsKeyDown(Keys.Down))
                {
                    selectedIndex = (selectedIndex + 1) % _menuItems;
                }
            }

            if (newState.IsKeyDown(Keys.Up))
            {
                if (!oldState.IsKeyDown(Keys.Up))
                {
                    selectedIndex = (selectedIndex + _menuItems - 1) % _menuItems;
                }
            }

            if (newState.IsKeyDown(Keys.Escape))
            {
                if (!oldState.IsKeyDown(Keys.Escape))
                {
                    myGame.currentScreen = CurrentScreen.Exit;
                }
            }

            if (newState.IsKeyDown(Keys.Enter))
            {
                if (!oldState.IsKeyDown(Keys.Enter))
                {
                    if (selectedIndex == 0)
                    {
                        myGame.currentScreen = CurrentScreen.ChooseCharacter;
                    }
                    if (selectedIndex == 1)
                    {
                        Global.BroadcastEvent(Global.EVENTS.GM_CONTINUE_GAME, this, null);
                    }
                    if (selectedIndex == 2)
                    {
                        myGame.currentScreen = CurrentScreen.Setting;
                    }
                    if (selectedIndex == 3)
                    {
                        myGame.currentScreen = CurrentScreen.Guide;
                    }
                    if (selectedIndex == 4)
                    {
                        myGame.currentScreen = CurrentScreen.Credit;
                    }
                    if (selectedIndex == 5)
                    {
                        myGame.currentScreen = CurrentScreen.Exit;
                    }
                }

            }
            oldState = newState;
        }

        public override void Update(GameTime gameTime)
        {
            HandleKeyInput();
            pointer.Update(gameTime);
        }

        public override void Draw(SpriteBatch sb, GameTime gameTime)
        {
            background.Draw(sb, gameTime);
            Vector2 indent = new Vector2(0, 80);
            for (int i = 0; i < myButtons.Count; i++)
            {
                //draw pointer
               
                if (selectedIndex == i)
                {
                    pointer.Draw(sb, gameTime, pointer.TopLeft + i * indent, pointer.Size);
                    myButtons[i].Draw(sb, gameTime);
                }
                else
                {
                    myButtons[i].Draw(sb, gameTime);
                }
            }
        }
    }
}
